package com.wuyou.mid;


/**
 * 长度为 K 的子数组的能量值 I
 *
 * @Date 2024/11/6 10:05
 * @Author 无忧
 */
public class ResultsArray {


    public static void main(String[] args) {
        ResultsArray resultsArray = new ResultsArray();
        int[] nums = {1};
        int k = 1;
        for (int i : resultsArray.sol_3(nums, k)) {
            System.out.print(i + "\t");
        }
    }

    public int[] sol_3(int[] nums, int k) {
        if (k == 1) {
            return nums;
        }
        int[] res_arr = new int[nums.length - k + 1];
        int res_idx = 0;
        int i = 0;
        while (i < nums.length - k + 1) {
            int j = i + 1, temp_i = i, count = 1;
            while (j < nums.length && nums[j] - nums[temp_i] == 1) {
                count++;
                j++;
                temp_i++;
            }
            while (count >= k) {
                count--;
                res_arr[res_idx++] = nums[i + k - 1];
                i++;
            }
            int t = j - i;
            while (t > 0 && res_idx < res_arr.length) {
                res_arr[res_idx++] = -1;
                t--;
            }
            i = j;
        }
        return res_arr;
    }

    public int[] sol_2(int[] nums, int k) {
        int[] res_arr = new int[nums.length - k + 1];
        int res_idx = 0, i = 0;
        while (i < nums.length - k + 1) {
            int temp_i = i, j = i + 1, temp_k = k - 1;
            while (j < nums.length && temp_k > 0 && nums[j] - nums[temp_i] == 1) {
                temp_k--;
                temp_i++;
                j++;
            }
            if (temp_k == 0) {
                res_arr[res_idx++] = nums[temp_i];
                i++;
                while (j < nums.length && nums[j++] - nums[temp_i++] == 1) {
                    res_arr[res_idx++] = nums[temp_i];
                    i++;
                }
            } else {
                int count = nums.length - j >= k ? j - i : res_arr.length - res_idx;
                while (res_idx < res_arr.length && count-- > 0) {
                    res_arr[res_idx++] = -1;
                }
                i = j;
            }
        }
        return res_arr;
    }


    public int[] sol_1(int[] nums, int k) {
        int[] res_arr = new int[nums.length - k + 1];
        for (int i = 0; i < nums.length - k + 1; i++) {
            int temp_i = i, j = i + 1, temp_k = k - 1, temp_max = nums[i];
            while (j < nums.length && temp_k > 0) {
                if (nums[j] - nums[temp_i] != 1) {
                    temp_max = -1;
                    break;
                }
                temp_max = nums[j];
                j++;
                temp_i++;
                temp_k--;
            }
            res_arr[i] = temp_max;
        }
        return res_arr;
    }
}
